CartPole-v0 example

does random actions, creates animated gif instead of display


In [24]:
%matplotlib inline
from JSAnimation.IPython_display import display_animation
from matplotlib import animation
import matplotlib.pyplot as plt
from IPython.display import display

import gym

env = gym.make('CartPole-v0')

# Run a demo of the environment
observation = env.reset()
cum_reward = 0
frames = []
for t in range(1000):
    # Render into buffer. 
    # You will still see the window.  ??? how do I avoid  display ???
    frames.append(env.render(mode = 'rgb_array', close=False))
    action = env.action_space.sample()
    observation, reward, done, info = env.step(action)
    if done:
        break
env.render(close=True)

def display_frames_as_gif(frames):
    """
    Displays a list of frames as a gif, with controls
    """
    plt.figure(figsize=(frames[0].shape[1] / 72.0, frames[0].shape[0] / 72.0), dpi = 72)
    patch = plt.imshow(frames[0])
    plt.axis('off')

    def animate(i):
        patch.set_data(frames[i])

    anim = animation.FuncAnimation(plt.gcf(), animate, frames = len(frames), interval=50)
    display(display_animation(anim, default_mode='loop'))

display_frames_as_gif(frames)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-24-db1f922459ac> in <module>()
     16     # Render into buffer.
     17     # You will still see the window.
---> 18     frames.append(env.render(mode = 'rgb_array', close=False))
     19     action = env.action_space.sample()
     20     observation, reward, done, info = env.step(action)

/usr/local/lib/python3.5/dist-packages/gym/core.py in render(self, mode, close)
    148             elif mode not in modes:
    149                 raise error.UnsupportedMode('Unsupported rendering mode: {}. (Supported modes for {}: {})'.format(mode, self, modes))
--> 150         return self._render(mode=mode, close=close)
    151 
    152     def close(self):

/usr/local/lib/python3.5/dist-packages/gym/core.py in _render(self, mode, close)
    284 
    285     def _render(self, mode='human', close=False):
--> 286         return self.env.render(mode, close)
    287 
    288     def _close(self):

/usr/local/lib/python3.5/dist-packages/gym/core.py in render(self, mode, close)
    148             elif mode not in modes:
    149                 raise error.UnsupportedMode('Unsupported rendering mode: {}. (Supported modes for {}: {})'.format(mode, self, modes))
--> 150         return self._render(mode=mode, close=close)
    151 
    152     def close(self):

/usr/local/lib/python3.5/dist-packages/gym/envs/classic_control/cartpole.py in _render(self, mode, close)
    113 
    114         if self.viewer is None:
--> 115             from gym.envs.classic_control import rendering
    116             self.viewer = rendering.Viewer(screen_width, screen_height)
    117             l,r,t,b = -cartwidth/2, cartwidth/2, cartheight/2, -cartheight/2

/usr/local/lib/python3.5/dist-packages/gym/envs/classic_control/rendering.py in <module>()
     21 
     22 try:
---> 23     from pyglet.gl import *
     24 except ImportError as e:
     25     reraise(prefix="Error occured while running `from pyglet.gl import *`",suffix="HINT: make sure you have OpenGL install. On Ubuntu, you can run 'apt-get install python-opengl'. If you're running on a server, you may need a virtual frame buffer; something like this should work: 'xvfb-run -s \"-screen 0 1400x900x24\" python <your_script.py>'")

/usr/local/lib/python3.5/dist-packages/pyglet/gl/__init__.py in <module>()
    222     else:
    223         from .carbon import CarbonConfig as Config
--> 224 del base
    225 
    226 # XXX remove

NameError: name 'base' is not defined

In [ ]: